package com.example.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @author adr
 */
public class AdbManager {

    public static List<String> devices = new ArrayList<>();
    public static Map<String, String> deviceIpToNames = new HashMap<>();
    public static List<Integer> portList = new ArrayList<>();

    public static void init() throws IOException {
        Process exec = Runtime.getRuntime().exec("adb devices");
        InputStream stream = exec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        String s = reader.readLine();
        List<String> list = new ArrayList<>();
        System.out.println("┌────────────────────────────────");
        System.out.println("│    ADMManager init()");
        while (s != null) {
            if (s.length() != 0 && s.endsWith("device")) {
                int i = s.indexOf("\t");
                if (i != -1) {
                    String s1 = s.substring(0, i);
                    list.add(s1);

                }
            }
            s = reader.readLine();
        }
        devices.clear();
        devices.addAll(list);
        for (String device : devices) {
            readEth0(device);
        }
        Set<Map.Entry<String, String>> set = deviceIpToNames.entrySet();
        set.forEach(entry -> {
            System.out.println("│    ip: " + wrapIpBlank(entry.getKey()) + "\tfind device: " + entry.getValue());

        });
        System.out.println("└────────────────────────────────");
    }

    private static String wrapIpBlank(String s) {
        int i = 15 - s.length();
        for (int i1 = 0; i1 < i; i1++) {
            s = s + " ";
        }
        return s;
    }

    public static void readEth0(String deviceName) throws IOException {
        Process exec = Runtime.getRuntime().exec("adb -s " + deviceName + " shell ifconfig eth0");
        InputStream stream = exec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        String s = reader.readLine();
        if (s == null) {
            readWlan0(deviceName);
            return;
        }
        putIpAndNames(deviceName, reader, s);
    }

    public static void readWlan0(String deviceName) throws IOException {
        Process exec = Runtime.getRuntime().exec("adb -s " + deviceName + " shell ifconfig wlan0");
        InputStream stream = exec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        String s = reader.readLine();
        putIpAndNames(deviceName, reader, s);
    }

    private static void putIpAndNames(String deviceName, BufferedReader reader, String s) throws IOException {
        while (s != null) {
            if (s.length() != 0) {
                int index = s.indexOf("inet addr:");
                if (index > 0) {
                    String ips = s.substring(index + 10);
                    int fb = ips.indexOf(" ");
                    String deviceIp = ips.substring(0, fb);
                    deviceIpToNames.put(deviceIp, deviceName);
                }
            }
            s = reader.readLine();
        }
    }

    public static String findMatchDeviceName(String name) throws IOException {
        init();
        String emName = deviceIpToNames.get(name);
        return emName == null ? "" : emName;
    }

    public static String findMatchDeviceNameDefault(String name) {
        String emName = deviceIpToNames.get(name);
        return emName == null ? "" : emName;
    }

    public static int register(String emName) throws IOException {
        System.out.println("------------ADMManager register start.--------------");
        int port;
        if (portList.size() == 0) {
            port = 5679;
        } else {
            portList.sort(Integer::compareTo);
            port = portList.get(portList.size() - 1) + 2;
        }
        Process exec = Runtime.getRuntime().exec("adb -s " + emName + " forward tcp:" + (port - 1) + " tcp:" + port);
        InputStream stream = exec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        String s = reader.readLine();
        if (s == null) {
            throw new IOException("adb forward port fail");
        }
        while (s != null) {
            if (s.length() != 0) {
                System.out.println("------------register result: " + s + "--------------");
            }
            s = reader.readLine();
        }
        portList.add(port);
        System.out.println("------------ADMManager register over.--------------");
        return port;
    }
}
